package moo.stargalaxy_bukkit.utils;

import com.alibaba.fastjson.JSONObject;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import moo.stargalaxy_bukkit.api.GalaxyAPI;
import moo.stargalaxy_bukkit.api.GalaxyBukkitAPI;
import moo.stargalaxy_bukkit.config.Config;
import moo.stargalaxy_bukkit.data.ServerData;
import org.bukkit.entity.Player;
import java.util.Map;
import static moo.stargalaxy_bukkit.utils.Lag.getTPS;


public class PaPi extends PlaceholderExpansion {
    @Override
    public boolean canRegister(){
        return true;
    }

    @Override
    public String getAuthor() {
        return "Moo QQ：1134802072";
    }

    @Override
    public String getIdentifier() {
        return "Galaxy";
    }

    @Override
    public String getVersion() {
        return "1.14.51.4";
    }

    @Override
    public String onPlaceholderRequest(Player player, String identifier){
        // return getSelfMessage(player, identifier);
        return New_getSelfMessage(player, identifier);

        //if(identifier.startsWith("Galaxysu")){
        //
        //}
        //if(identifier.startsWith("Galaxyzm")){
        //    return getOtherMessage(identifier);
        //}
        //return "";
    }

    // 此方法已启用 - 仅保留
    public static String getOtherMessage(String key){
        if(key.equals("player_total")){
            return String.valueOf(Config.getServerDataMap().size());
        }
        if(key.equals("version")){
            return Config.version;
        }
        String[] data = key.split("_");
        System.out.println("变量读取调试输出：" + data.toString());
        if(data[0].length() < 20){
            if(data.length != 2 || !data[1].equals("fid")) return "";
            int i = Integer.parseInt(data[1]);
            if(Config.getServerList().size() >= i){
                return Config.getServerList().get(i);
            }
            return "None-Server";
        }
        if(Config.getServerDataMap().containsKey(data[0])){
            ServerData serverData = Config.getServerDataMap().get(Config.getFid());
            switch (data[1]) {
                case "fid":
                case "name":
                    if(data.length != 2) return "";
                    return serverData.getServerName();
                case "createtime":
                    if(data.length != 2) return "";
                    return serverData.getCreateTime();
                case "onlineplayer":
                    if(data.length != 2) return "";
                    return serverData.getOnlinePlayers();
                case "maxplayer":
                    if(data.length != 2) return "";
                    return serverData.getMaxPlayers();
                case "owner":
                    if(data.length != 3) return "";
                    switch (data[2]){
                        case "name":
                            return serverData.getOwnerName();
                        case "uuid":
                            return serverData.getOwnerUUID();
                        default:
                            return "Error-Fid";
                    }
                case "run":
                    if(data.length != 3) return "Null-Fid";
                    switch (data[2]){
                        case "sataus":
                            return serverData.getRunState();case "status":
                        default:
                            return "Error-Fid";
                    }
                default:
                    return "Error-Fid";
            }
        }
       return "Error-Fid";
    }

    // 此方法已启用 - 仅保留
    public static String getSelfMessage(Player player, String key){
        // 分割变量内每个下划线
        String[] data = key.split("_");
        // 判断配置文件内FID是否争正确(弃用)
        // if(!Config.getServerDataMap().containsKey(Config.getFid())){ return "Error-Fid"; }

        if(key.equals("fid")){
            return Config.getConfiguration().getString("Server_Set.Server_Fid");
        }
        if(key.equals("player_total")){
            JSONObject playerServer = GalaxyAPI.getPlayerServer(player.getUniqueId().toString());
            if(playerServer.containsKey("datas")){
                // return String.valueOf(playerServer.getJSONObject("datas").getJSONObject("servers").getInnerMap().size());
                return String.valueOf(playerServer.getJSONObject("datas").getString("amount"));
            }
            return "0";
        }

        // 无用统计
        //        if(key.equals("player_total")){
        //            return String.valueOf(Config.getServerList().size());
        //        }
        ServerData serverData = Config.getServerDataMap().get(Config.getFid());

        System.out.println("变量读取调试输出：" + data.toString());
        if(data[0].length() < 20){
            if(data.length != 2 || !data[1].equals("fid")) return "";
            int i = Integer.parseInt(data[1]);
            if(Config.getServerList().size() >= i){
                return Config.getServerList().get(i);
            }
            return "None-Server";
        }

        switch (data[0]){
            case "fid":
                return serverData.getServerName();
            case "owner":
                if(data[1].equals("name")){
                    return serverData.getOwnerName();
                }
                if(data[1].equals("uuid")){
                    return serverData.getOwnerUUID();
                }
            case "server":
                if(data[1].equals("createtime")){
                    return serverData.getCreateTime();
                }
                if(data[1].equals("onlineplayer")){
                    return serverData.getOnlinePlayers();
                }
                if(data[1].equals("maxplayer")){
                    return serverData.getMaxPlayers();
                }
            default:
                if(Config.getFid().equals("none")) return "Null-Fid";
                return getOtherMessage(key); // 获取来自其他FID的信息
        }
    }


    // 此方法为正在使用的方法
    public static String New_getSelfMessage(Player player, String key){
        // 获取来自本体插件配置文件的FID识别码
        if(key.equals("fid")){
            return Config.getConfiguration().getString("Server_Set.Server_Fid");
        }
        // 获取玩家全部的子服数量
        if(key.equals("player_total")){
            JSONObject playerServer = GalaxyAPI.getPlayerServer(player.getUniqueId().toString());
            if(playerServer.containsKey("datas")){
                // return String.valueOf(playerServer.getJSONObject("datas").getJSONObject("servers").getInnerMap().size());
                return String.valueOf(playerServer.getJSONObject("datas").getString("amount"));
            }
            return "0";
        }

        if(key.equals("owner_name")){
            // 读取本地SID的信息：读取本子服服主名字
            ServerData serverData = Config.getServerDataMap().get(Config.getFid());
            return serverData.getOwnerName();
        }
        if(key.equals("owner_uuid")){
            // 读取本地SID的信息：读取本子服服主识别码
            ServerData serverData = Config.getServerDataMap().get(Config.getFid());
            return serverData.getOwnerUUID();
        }
        if(key.equals("server_createtime")){
            // 读取本地SID的信息：读取本子服创建时间
            ServerData serverData = Config.getServerDataMap().get(Config.getFid());
            return serverData.getCreateTime();
        }
        if(key.equals("server_onlineplayer")){
            // 读取本地SID的信息：读取本子服在线玩家数
            ServerData serverData = Config.getServerDataMap().get(Config.getFid());
            return serverData.getOnlinePlayers();
        }
        if(key.equals("server_maxplayer")){
            // 读取本地SID的信息：读取本子服在线玩家数
            ServerData serverData = Config.getServerDataMap().get(Config.getFid());
            return serverData.getMaxPlayers();
        }
        if(key.equals("real_tps")){
            // 获取当前服务端的实时Tps
            return String.valueOf(getTPS());
        }
        if(key.equals("limit_tps")){
            // 获取当前服务端的限制进入Tps
            return String.valueOf(Config.getConfiguration().getBoolean("Server_Set.TPS_JoinLimit"));
        }
        if(key.equals("max_createserver_snumber")){
            // 获取最大可创建服务器数量
            // 先判断有没有通过权限设置数量，没有的话就从配置中取得默认数量作为创建数量
            int canCreateServer = GalaxyBukkitAPI.getCanCreateServer(player);
            if (canCreateServer == 0) {
                canCreateServer = Config.getConfiguration().getInt("Server_Set.Default_Snumber");
            }
            return String.valueOf(canCreateServer);
        }
        if(key.equals("for_createserver_snumber")){
            // 判断是否可继续创建服务器-判断是否玩家满额 返回布尔值 (true / false)
            // 先判断有没有通过权限设置数量，没有的话就从配置中取得默认数量作为创建数量
            JSONObject playerServer = GalaxyAPI.getPlayerServer(player.getUniqueId().toString());
            if(playerServer.containsKey("datas")){
                // 读取玩家已经创建的数量
                int player_total = playerServer.getJSONObject("datas").getIntValue("amount");
                    // 读取本地权限设置的上限和来自配置文件的默认设置
                    int canCreateServer = GalaxyBukkitAPI.getCanCreateServer(player);
                    if (canCreateServer == 0) {
                        canCreateServer = Config.getConfiguration().getInt("Server_Set.Default_Snumber");
                    }
                // 判断是否已用的打到上限
                if (player_total >= canCreateServer) {
                    return "false";
                } else {
                    return "true";
                }
            }
            return "false";
        }

        // (此处开始判断变量内包含SID的内容) 分割变量内每个下划线 =================================================================================================
        String[] data = key.split("_");

        // 获取特定子服序号的识别码
        if (data.length == 2 && data[1].equals("fid")) {
            JSONObject playerServer = GalaxyAPI.getPlayerServer_sel(player.getUniqueId().toString() ,data[0]);
            if(playerServer.containsKey("datas")){
                return String.valueOf(playerServer.getJSONObject("datas").getString("子服编号"));
            }
            return "None-Server";
         }
        // 获取特定子服序号的显示名字
        if (data.length == 2 && data[1].equals("name")) {
            // 从云端读取特定子服数据
            JSONObject serverInfo = GalaxyAPI.getServerInfo(data[0]);
            // 判断获取数据的状态
            if(GalaxyAPI.isConnect(serverInfo)){
                Map o = serverInfo.getJSONObject("datas").getObject("servers", Map.class);
                return o.get("显示名称").toString();
            } else {
                return "None-Server";
            }
        }
        // 获取特定子服序号的创建时间
        if (data.length == 2 && data[1].equals("createtime")) {
            // 从云端读取特定子服数据
            JSONObject serverInfo = GalaxyAPI.getServerInfo(data[0]);
            // 判断获取数据的状态
            if(GalaxyAPI.isConnect(serverInfo)){
                Map o = serverInfo.getJSONObject("datas").getObject("servers", Map.class);
                return o.get("创建时间").toString();
            } else {
                return "None-Server";
            }
        }
        // 获取特定子服序号的在线玩家数
        if (data.length == 2 && data[1].equals("onlineplayer")) {
            // 从云端读取特定子服数据
            JSONObject serverInfo = GalaxyAPI.getServerInfo(data[0]);
            // 判断获取数据的状态
            if(GalaxyAPI.isConnect(serverInfo)){
                Map o = serverInfo.getJSONObject("datas").getObject("servers", Map.class);
                return o.get("在线人数").toString();
            } else {
                return "None-Server";
            }
        }
        // 获取特定子服序号的最大玩家数
        if (data.length == 2 && data[1].equals("maxplayer")) {
            // 从云端读取特定子服数据
            JSONObject serverInfo = GalaxyAPI.getServerInfo(data[0]);
            // 判断获取数据的状态
            if(GalaxyAPI.isConnect(serverInfo)){
                Map o = serverInfo.getJSONObject("datas").getObject("servers", Map.class);
                return o.get("最大人数").toString();
            } else {
                return "None-Server";
            }
        }
        // 获取特定子服序号的子服服主名字
        if (data.length == 3 && data[1].equals("owner") && data[2].equals("name")) {
            // 从云端读取特定子服数据
            JSONObject serverInfo = GalaxyAPI.getServerInfo(data[0]);
            // 判断获取数据的状态
            if(GalaxyAPI.isConnect(serverInfo)){
                Map o = serverInfo.getJSONObject("datas").getObject("servers", Map.class);
                return o.get("玩家名称").toString();
            } else {
                return "None-Server";
            }
        }
        // 获取特定子服序号的子服服主识别码
        if (data.length == 3 && data[1].equals("owner") && data[2].equals("uuid")) {
            // 从云端读取特定子服数据
            JSONObject serverInfo = GalaxyAPI.getServerInfo(data[0]);
            // 判断获取数据的状态
            if(GalaxyAPI.isConnect(serverInfo)){
                Map o = serverInfo.getJSONObject("datas").getObject("servers", Map.class);
                return o.get("玩家识别码").toString();
            } else {
                return "None-Server";
            }
        }
        // 获取特定子服序号的子服状态显示文本
        if (data.length == 3 && data[1].equals("run") && data[2].equals("sataustext")) {
            // 从云端读取特定子服数据
            JSONObject serverInfo = GalaxyAPI.getServerInfo(data[0]);
            // 判断获取数据的状态
            if(GalaxyAPI.isConnect(serverInfo)){
                Map o = serverInfo.getJSONObject("datas").getObject("servers", Map.class);
                return o.get("状态文本").toString();
            } else {
                return "None-Server";
            }
        }
        // 获取特定子服序号的子服状态代码
        if (data.length == 3 && data[1].equals("run") && data[2].equals("satauscode")) {
            // 从云端读取特定子服数据
            JSONObject serverInfo = GalaxyAPI.getServerInfo(data[0]);
            // 判断获取数据的状态
            if(GalaxyAPI.isConnect(serverInfo)){
                Map o = serverInfo.getJSONObject("datas").getObject("servers", Map.class);
                return o.get("运行状态").toString();
            } else {
                return "None-Server";
            }
        }
        // 获取特定子服序号的实时TPS
        if (data.length == 3 && data[1].equals("real") && data[2].equals("tps")) {
            // 从云端读取特定子服数据
            JSONObject serverInfo = GalaxyAPI.getServerInfo(data[0]);
            // 判断获取数据的状态
            if(GalaxyAPI.isConnect(serverInfo)){
                Map o = serverInfo.getJSONObject("datas").getObject("servers", Map.class);
                return o.get("实时流畅度").toString();
            } else {
                return "None-Server";
            }
        }
        // 获取特定子服序号的限制TPS
        if (data.length == 3 && data[1].equals("limit") && data[2].equals("tps")) {
            // 从云端读取特定子服数据
            JSONObject serverInfo = GalaxyAPI.getServerInfo(data[0]);
            // 判断获取数据的状态
            if(GalaxyAPI.isConnect(serverInfo)){
                Map o = serverInfo.getJSONObject("datas").getObject("servers", Map.class);
                return o.get("限制流畅度").toString();
            } else {
                return "None-Server";
            }
        }


        return "Null-Placeholder";
    }

}
